package com.tappytaps.android.hc2010;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.TimeZone;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class VocabularyListActivity extends ListActivity {
	private DateFormat shortTimeFormater = DateFormat
			.getTimeInstance(DateFormat.SHORT);
	private DateFormat shortDateFormater = DateFormat
			.getDateInstance(DateFormat.SHORT);
	
	public static final String TYPE_NORMAL = "1";
	public static final String TYPE_HEADER = "2";

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		HashMap<String, String> a = (HashMap<String, String>) l
				.getItemAtPosition(position);
//		long idDategroup = Long.valueOf(a.get("idWord"));

		super.onListItemClick(l, v, position, id);
	}

	private static ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
	private static HashMap<String, String> item;

	// From API Demos
	private static class EfficientAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		Context ctx;

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.ctx = context;
		}

		/**
		 * The number of items in the list is determined by the number of
		 * speeches in our array.
		 * 
		 * @see android.widget.ListAdapter#getCount()
		 */
		public int getCount() {
			return list.size();
		}

		/**
		 * Since the data comes from an array, just returning the index is
		 * sufficent to get at the data. If we were using a more complex data
		 * structure, we would return whatever object represents one row in the
		 * list.
		 * 
		 * @see android.widget.ListAdapter#getItem(int)
		 */
		public Object getItem(int position) {
			return list.get(position);
		}

		/**
		 * Use the array index as a unique id.
		 * 
		 * @see android.widget.ListAdapter#getItemId(int)
		 */
		public long getItemId(int position) {
			return position;
		}

		/**
		 * Make a view to hold each row.
		 * 
		 * @see android.widget.ListAdapter#getView(int, android.view.View,
		 *      android.view.ViewGroup)
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
			// A ViewHolder keeps references to children views to avoid
			// unneccessary calls
			// to findViewById() on each row.
			ViewHolder holder;

			// When convertView is not null, we can reuse it directly, there is
			// no need
			// to reinflate it. We only inflate a new View when the convertView
			// supplied
			// by ListView is null.
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.vocabulary_list_item, null);

				// Creates a ViewHolder and store references to the two children
				// views
				// we want to bind data to.
				
				holder = new ViewHolder();
				holder.vocabularyHeaderRow = (LinearLayout) convertView.findViewById(R.id.VocabularyHeaderRow);
				holder.vocabularyNormalRow = (LinearLayout) convertView.findViewById(R.id.VocabularyNormalRow);
				holder.original = (TextView) convertView.findViewById(R.id.original);
				holder.translation = (TextView) convertView.findViewById(R.id.translation);
				
				convertView.setTag(holder);
			} else {
				// Get the ViewHolder back to get fast access to the TextView
				// and the ImageView.
				holder = (ViewHolder) convertView.getTag();
			}

			// Bind the data efficiently with the holder.
			item = list.get(position);
			String rowType = item.get("type");

			if (rowType.equals(TYPE_NORMAL)) {
				holder.vocabularyHeaderRow.setVisibility(View.GONE);				
				holder.vocabularyNormalRow.setVisibility(View.VISIBLE);
				holder.original.setText(item.get("original"));
				holder.translation.setText(item.get("translation"));
			} else {
				// TODO type header
				holder.vocabularyHeaderRow.setVisibility(View.VISIBLE);
				holder.vocabularyNormalRow.setVisibility(View.GONE);
				
			}
			
			return convertView;
		}

		static class ViewHolder {
			LinearLayout vocabularyHeaderRow;
			LinearLayout vocabularyNormalRow;
			
			TextView original;
			TextView translation;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
		setContentView(R.layout.vocabulary_list_activity);
	}

	@Override
	public void onResume() {
		fillData();
		setListAdapter(new EfficientAdapter(this));

		super.onResume();
	}

	private void fillData() {
		Database database = new Database(this);
		database.open();

		Cursor words = database.fetchAllWords();

		if (words.getCount() == 0) {
			words.close();
			database.close();
			list = new ArrayList<HashMap<String, String>>();
			return;
		}

		list = new ArrayList<HashMap<String, String>>();

		
		int indexRowId = words.getColumnIndex(Database.KEY_WORD_ROWID);
		int indexOriginal = words.getColumnIndex(Database.KEY_WORD_ORIGINAL);
		int indexTranslation = words.getColumnIndex(Database.KEY_WORD_TRANSLATION);
		int indexPriority = words.getColumnIndex(Database.KEY_WORD_PRIORITY);
		int indexIdCategory = words.getColumnIndex(Database.KEY_WORD_ID_CATEGORY);
		int indexLastUpdate = words.getColumnIndex(Database.KEY_WORD_LAST_UPDATE);
		int indexCreated = words.getColumnIndex(Database.KEY_WORD_CREATED);
		
		
		long rowId;
		String original, translation;
		int priority;
		int idCategory;
		long lastUpdate, created;
		
		
		words.moveToFirst();
		while (!words.isAfterLast()) {
			rowId = words.getLong(indexRowId); 
			original = words.getString(indexOriginal);
			translation = words.getString(indexTranslation);
			priority = words.getInt(indexPriority);
			idCategory = words.getInt(indexIdCategory);
			lastUpdate = words.getLong(indexLastUpdate);
			created = words.getLong(indexCreated);
			
			item = new HashMap<String, String>();

			item.put("type", TYPE_NORMAL);
			item.put("idWord", String.valueOf(rowId));
			item.put("idCategory", String.valueOf(idCategory));
			item.put("original", original);
			item.put("translation", translation);
			item.put("priority", String.valueOf(priority));
			//item.put("idCategory", String.valueOf(idCategory));
			//item.put("lastUpdate", lastUpdate);
			
			list.add(item);
			
			words.moveToNext();
		}

		words.close();
		database.close();
	}

	
	
	   // MENU
	   @Override
	   public boolean onCreateOptionsMenu(Menu menu) {
	       MenuInflater inflater = getMenuInflater();
	       inflater.inflate(R.menu.vocabulary_menu, menu);
	       return true;
	   }
	   
	   
	   @Override
	   public boolean onOptionsItemSelected(MenuItem item) {
	       // Handle item selection
	       switch (item.getItemId()) {
	       case R.id.add_item:
	           Intent addWord = new Intent(VocabularyListActivity.this, AddWordActivity.class);
	           startActivity(addWord);
	           return true;
	       default:
	           return super.onOptionsItemSelected(item);
	       }
	   }
}
